using System;

namespace OpenComposite.Base
{
	/// <summary>
	/// Exception for Base projects.
	/// </summary>
	public class BaseException : ApplicationException 
	{
		#region Members
		
		#region Private Vars

		/// <summary>
		/// Error number.
		/// </summary>
		private int m_ErrorNumber = 0;

		/// <summary>
		/// Error message.
		/// </summary>
		private string m_ErrorMessage = "";

		#endregion Private Vars

		#region Private Methods
		#endregion Private Methods

		#endregion

		#region Interface

		#region Properties

		/// <summary>
		/// Get Error Message.
		/// </summary>
		public override string Message
		{
			get {return m_ErrorMessage; }
		}


		/// <summary>
		/// Get Error Number.
		/// </summary>
		public int ErrorNumber
		{
			get { return m_ErrorNumber; }
		}


		#endregion Properties

		#region Methods

		/// <summary>
		/// Returns a String that represents the current object.
		/// </summary>
		/// <returns>A String that represents the current object.</returns>
		public override string ToString()
		{
			return this.Message + " [" + this.ErrorNumber.ToString() + "]";
		}


		#endregion Methods

		#endregion Interface

		#region Statics

		/// <summary>
		/// Concatenate error messages of all inner exceptions.
		/// </summary>
		/// <param name="exception">Top level exception.</param>
		/// <param name="delimiter">Delimiter string.</param>
		/// <returns></returns>
		public static string JoinErrorMessages(Exception exception, string delimiter)
		{
			string msg = "";
			Exception inner = exception;
			while(inner != null)
			{
				msg += inner.Message + delimiter;
				inner = inner.InnerException;
			}
			return msg;
		}


		#endregion Statics

		#region Contructors

		/// <summary>
		/// BaseException contructor.
		/// </summary>
		public BaseException()
		{
		}


		/// <summary>
		/// BaseException contructor.
		/// </summary>
		/// <param name="errorNumber">Specific error number.</param>
		public BaseException(int errorNumber)
		{
			m_ErrorNumber = errorNumber;
		}


		/// <summary>
		/// BaseException contructor.
		/// </summary>
		/// <param name="errorNumber">Specific error number.</param>
		/// <param name="innerException">Inner exception.</param>
		public BaseException(int errorNumber, Exception innerException)
			: base("", innerException)
		{
			m_ErrorNumber = errorNumber;
		}


		/// <summary>
		/// BaseException contructor.
		/// </summary>
		/// <param name="errorNumber">Specific error number.</param>
		/// <param name="errorMessage">Message string.</param>
		public BaseException(int errorNumber, string errorMessage)
		{
			m_ErrorNumber = errorNumber;
			m_ErrorMessage = errorMessage;
		}


		/// <summary>
		/// BaseException contructor.
		/// </summary>
		/// <param name="errorNumber">Specific error number.</param>
		/// <param name="errorMessage">Message string.</param>
		/// <param name="innerException">Inner exception.</param>
		public BaseException(int errorNumber, string errorMessage, Exception innerException)
			: base("", innerException)
		{
			m_ErrorNumber = errorNumber;
			m_ErrorMessage = errorMessage;
		}


    	/// <summary>
		/// BaseException contructor.
		/// </summary>
		/// <param name="errorNumber">Specific error number.</param>
		/// <param name="errorMessage">Message string with parameter placeholders.</param>
		/// <param name="innerException">Inner exception.</param>
		/// <param name="parameters">Parameters to fill error message string.</param>
		public BaseException(int errorNumber, string errorMessage, Exception innerException, params object[] parameters)
			: base("", innerException)
		{
			m_ErrorNumber = errorNumber;
			m_ErrorMessage = String.Format(errorMessage, parameters);
		}


		#endregion Contructors
	}
}
